package com.pap.rbac.userfield.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.dataclazz.IDataClazzAgent;
import com.pap.rbac.agent.datafield.IDataFieldAgent;
import com.pap.rbac.agent.datamodule.IDataModuleAgent;
import com.pap.rbac.agent.dataproject.IDataProjectAgent;
import com.pap.rbac.agent.userfield.IUserFieldPropAgent;
import com.pap.rbac.agent.userfield.dto.UserFieldPropWithExtraPropDTO;
import com.pap.rbac.dataclazz.auto.entity.DataClazz;
import com.pap.rbac.datafield.auto.entity.DataField;
import com.pap.rbac.datamodule.auto.entity.DataModule;
import com.pap.rbac.dataproject.auto.entity.DataProject;
import com.pap.rbac.userfield.auto.entity.UserFieldProp;

@RestController
@RequestMapping("/v1/rbac/userfieldprop")
public class UserFieldPropController extends BaseController<UserFieldProp, IUserFieldPropAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(UserFieldPropController.class);
	
	@Resource(name = "userFieldPropAgent")
	@Override
	public void setService(IUserFieldPropAgent agent) {
		super.service = agent;
	}
	
	@Autowired
	private IDataProjectAgent dataProjectAgent;
	
	@Autowired
	private IDataModuleAgent dataModuleAgent;
	
	@Autowired
	private IDataClazzAgent dataClazzAgent;
	
	@Autowired
	private IDataFieldAgent dataFieldAgent;
	
	@Override
	@RequestMapping(value = "/add")
	public ResponseVO<UserFieldProp> add(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "addJSON", required = true) String addJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		UserFieldProp e = objectMapper.readValue(addJSON, UserFieldProp.class);
		if(StringUtilss.isEmpty(e.getId())) {
			e.setId(UUID.randomUUID().toString());
		}
		e.setCreateIp("0.0.0.0");
		e.setCreateTime(DateUtils.getCurrDateTimeStr());
		e.setCreateUser(loginUserId);
		e.setClientLicenseId(clientLicenseId);

		int operationInt = service.insertSelective(e);
		return ResponseVO.successdata(operationInt);
	}

	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping("/querypage")
	public ResponseVO<UserFieldProp> queryPage(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "queryJSON", required = true) String queryJSON,
			@RequestParam(value = "pageJSON", required = true) String pageJSON) throws Exception {

		ObjectMapper mapper = JacksonUtilss.getObjectMapper();
		
		Map<Object, Object> searchMap = new HashMap<Object, Object>();
		
		Map<Object, Object> queryMap = mapper.readValue(queryJSON, Map.class);
		Map<Object, Object> pageMap = mapper.readValue(pageJSON, Map.class);
		
		searchMap.putAll(queryMap);
		
		Page page = null;
		if(pageMap != null && !pageMap.isEmpty() && pageMap.containsKey("page") && pageMap.containsKey("rows")) {
			page = new Page(pageMap.get("page").toString(), pageMap.get("rows").toString(), 0);
			searchMap.put("page", page);
		} else {
			page = new Page("1", "10", 0);
		}
		searchMap.put("clientLicenseId", clientLicenseId);
		
		List<UserFieldPropWithExtraPropDTO> dataWithParent = new ArrayList<UserFieldPropWithExtraPropDTO>();
		
		List<UserFieldProp> operationList =  service.selectListByMap(searchMap);
		if(operationList != null && operationList.size() > 0) {
			for (UserFieldProp userFieldProp : operationList) {
				UserFieldPropWithExtraPropDTO dtoTemp = new UserFieldPropWithExtraPropDTO();
				BeanUtils.copyProperties(userFieldProp, dtoTemp);
				DataProject projectTemp = dataProjectAgent.selectByPrimaryKey(userFieldProp.getProjectId());
				if(projectTemp != null) {
					dtoTemp.setDataProjectName(projectTemp.getName());
				}
				DataModule moduleTemp = dataModuleAgent.selectByPrimaryKey(userFieldProp.getModuleId());
				if(moduleTemp != null) {
					dtoTemp.setDataModuleName(moduleTemp.getName());
				}
				DataClazz clazzTemp = dataClazzAgent.selectByPrimaryKey(userFieldProp.getClazzId());
				if(moduleTemp != null) {
					dtoTemp.setDataClazzName(clazzTemp.getName());
				}
				DataField fieldTemp = dataFieldAgent.selectByPrimaryKey(userFieldProp.getFieldId());
				if(moduleTemp != null) {
					dtoTemp.setDataFieldName(fieldTemp.getName());
				}
				dataWithParent.add(dtoTemp);
				
			}
		}
		
		int count = service.selectCountByMap(searchMap);
		page = new Page(page.getCurrent() + "", page.getLength() + "", count);
		
		return ResponseVO.successdatas(dataWithParent, page);
	}
}
